package com.fengye.jpa.converter.controller;

import com.fengye.jpa.converter.model.many2many.Book;
import com.fengye.jpa.converter.model.Student;
import com.fengye.jpa.converter.respository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/list")
    public List<Student> getAll(){
        return studentRepository.findAll();
    }

    @PostMapping("save")
    public Student save(){
        Student student = new Student();
        List<Book> books = new ArrayList<>();
        Book book = new Book();
        book.setId(1L);
        book.setName("java编程思想");
//        book.setAuthor("fengye");
        books.add(book);
        student.setBooks(books);
//        student.setId();
        return studentRepository.save(student);
    }
}
